<template>
  <div class="app-container">
    <el-button type="success" @click="additem('1')">+新增菜品分类</el-button>
    <el-button type="warning" @click="additem('2')">+新增套餐分类</el-button>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      style="margin-top:20px"
    >
      <el-table-column align="center" label="分类名称" width="200">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="分类类型" width="200">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column label="操作时间" width="250" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" width="250" align="center">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-link type="primary" style="margin-right:20px" @click="dialogFormVisiblelist(scope.row)">修改</el-link>
          <el-link type="danger" @click="deleteditem(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="修改分类" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="分类名称：" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="排序：" :label-width="formLabelWidth">
          <el-input v-model="form.sort" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisibles('取消')">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisibles('确定')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryPage, deleCategory, editCategory, addCategory } from '@/api/catefory'
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 40,
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '100px',
      type: 0,
      xixi: ''
    }
  },
  async mounted() {
    this.selectTable()
  },
  methods: {
    async selectTable() {
      this.listLoading = true
      const response = await getCategoryPage({
        page: this.currentPage,
        pageSize: this.pageSize
      })
      this.list = response.records
      this.currentPage = response.current
      this.pageSize = response.size
      this.total = response.total
      this.listLoading = false
    },
    // 每页条数
    handleSizeChange(val) {
      this.pageSize = val
      this.selectTable()
    },
    // 当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.selectTable()
    },
    // 删除单条
    deleteditem(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deleCategory(id).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          this.selectTable()
        } catch {
          console.log('error deleted')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改
    async dialogFormVisiblelist(e) {
      this.xixi = 'xixi'
      this.dialogFormVisible = true
      this.form = e
    },
    // 新增菜品，新增套餐分类
    async additem(e) {
      this.dialogFormVisible = true
      this.type = e
      this.xixi = 'add'
    },
    // 确定取消按钮：新增和修改
    async dialogFormVisibles(e) {
      this.dialogFormVisible = false
      if (e === '确定') {
        if (this.xixi === 'add') {
          try {
            await addCategory({ name: this.form.name, sort: this.form.sort, type: this.type })
          } catch {
            console.error('error,wrong')
          }
        } else {
          await editCategory({ id: this.form.id, name: this.form.name, sort: this.form.sort })
        }
        this.selectTable()
        this.form = {}
      } else if (e === '取消') {
        this.form = {}
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.block{
  display: flex;
  justify-content: center;
  padding: 20px 0;
}
</style>
